package com.minelx.sqlstructures.statements.parse.select;

import com.minelx.sequence.core.ReturnMatcher;
import com.minelx.sequence.core.StringScanner;
import com.minelx.sqlstructures.statements.SelectBuilder;
import com.minelx.sqlstructures.statements.clause.AndBuilder;
import com.minelx.sqlstructures.statements.clause.Sql;
import com.minelx.sqlstructures.statements.parse.select.where.AndBuilderParser;

import java.util.ArrayList;

public class SelectBuilderParser {
	private final SelectClauseBuilderParser selectClauseBuilderParser;

	private final ReturnMatcher<AndBuilder, StringScanner> andBuilderParser;

	private final ReturnMatcher<Sql, StringScanner> limitParser;

	public SelectBuilderParser() {
		selectClauseBuilderParser = new SelectClauseBuilderParser();
		andBuilderParser = AndBuilderParser.create();
		limitParser = Parsers.limit();
	}

	public SelectBuilder parse(String sql) {
		return parse(StringScanner.source(sql));
	}

	public SelectBuilder parse(StringScanner source) {
		return new SelectBuilder(
				selectClauseBuilderParser.get(source).orElseThrow(RuntimeException::new),
				andBuilderParser.get(source).orElse(AndBuilder.empty()),
				Sql.empty(),
				limitParser.get(source).orElse(Sql.empty()));
	}
}
